package com.ciwong.epaper.modules.wordlist;


import android.content.Intent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.Answer;
import com.ciwong.epaper.modules.me.bean.WorkContents;
import com.ciwong.epaper.modules.wordlist.bean.PageListBean;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.widget.RoundProgressBar;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.mobilelib.bean.UserInfoBase;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.BaseIntentFlag;

import java.util.ArrayList;
import java.util.Timer;

/**
 * Created by young on 2018/3/27.
 */

public abstract class BaseWordlistActivity extends BaseActivity implements View.OnClickListener {

    protected float score;

    public ArrayList<PageListBean> words;

    protected int position;

    protected Answer answer;

    protected boolean isPlayAssess = true;// 是否播放评价语


    protected Timer timer;

    protected boolean isTimering;// 是否正在计时

    protected ImageView playing, recording;//
    protected RoundProgressBar round_progress_bar;//播放进度

    protected TextView tvNo, tv_count;
    protected ProgressBar progressBar;//整个试卷的进度显示
    protected WordListBottomView read_bottom_view;

    protected UserInfoBase userInfo;

    protected int mServiceId;

    protected WorkContents mWorkContents;

    protected int moduleId;
    protected TextView repeat_btn, next_word_btn;
    protected int mRequestFrom;

    @Override
    protected void preCreate() {
        super.preCreate();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息栏
        Intent intent = getIntent();
        if (intent != null) {
            //
            words = new ArrayList<PageListBean>();
            words = (ArrayList<PageListBean>) intent.getSerializableExtra(BaseIntentFlag.INTENT_FLAG_OBJ_LIST);
            mRequestFrom = getIntent().getIntExtra(IntentFlag.INTENT_FLAG_REQUEST_CODE_FROM, -1);
        }

    }

    @Override
    protected void findViews() {
        round_progress_bar = findViewById(R.id.round_progress_bar);
        playing = findViewById(R.id.problemPlay);
        recording = findViewById(R.id.img_micro_recording);
        /*tvNo = findViewById(R.id.btn_repeat_read_no);
        tv_count = findViewById(R.id.btn_repeat_read_count);
        progressBar = findViewById(R.id.progressBar);
        read_bottom_view = findViewById(R.id.read_bottom_view);*/
        repeat_btn = findViewById(R.id.repeat_btn);
        next_word_btn = findViewById(R.id.next_word_btn);

    }


    @Override
    protected void init() {

    }

    @Override
    protected void initEvent() {
        repeat_btn.setOnClickListener(this);
        next_word_btn.setOnClickListener(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        isPlayAssess = CWSys.getSharedBoolean(
                EConstants.SHARE_KEY_IS_ASSESS + getUserInfoBase().getUserId(),
                false);
    }


    /**
     * 取消计时
     */
    protected void cancelTimer() {
        if (timer != null && isTimering) {
            timer.cancel();
            isTimering = false;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    public abstract void goBack();


    /**
     * 设置题号和做题进度
     *
     * @param index
     * @param total
     */
    public void setNo(int index, int total) {
        tvNo.setText("" + index);
        tv_count.setText("/" + total);
        progressBar.setMax(total);
        progressBar.setProgress(index);
    }


    /**
     * 切换到播放状态
     */
    public void switchPlayState() {

    }

    /**
     * 切换到重启状态
     */
    public void switchRestartState() {
        read_bottom_view.setInitState();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {

    }

    @Override
    public void onBackPressed() {
        BaseWordlistActivity.this.goBack();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.next_word_btn) {
            nextWord();
        } else if (v.getId() == R.id.repeat_btn) {
            repeatWord();
        }

    }

    static class AnimListener implements Animation.AnimationListener {
        private View view;

        private boolean hideAfter;

        public AnimListener(View v, boolean hide) {
            view = v;
            hideAfter = hide;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            //如果点击再次跟读后，禁止提交，动画完成再使能该提交答案控件
            //view.findViewById(R.id.speech_submit_work).setEnabled(true);
            if (hideAfter) {
                view.clearAnimation();
                view.setVisibility(View.GONE);
            }

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationStart(Animation animation) {

        }
    }

    /**
     * 播放事件
     */
    public abstract void play();

    /**
     * 暂停事件
     */
    public abstract void pause();

    /**
     * 下一个单词
     */
    public abstract void nextWord();

    /**
     * 重读这个单词
     */
    public abstract void repeatWord();


}
